﻿using EBA.Framework;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;

namespace EBA.Tests.Framework
{
    [TestClass]
    public class BundleRuntimeTest
    {

        [TestMethod]
        public void Test_Start_Bundle_With_Config()
        {
            using (var runtime = new BundleRuntime<IBundle>())
            {
                runtime.Start("Bundle");

                Assert.AreEqual("Bundle", runtime.BundleContextes.Select(i => i.Bundle.Id).FirstOrDefault());

            }
        }

        [TestMethod]
        public void Test_Start_RemotingProcessBundle_With_Config()
        {
            using (var runtime = new BundleRuntime<IBundle>())
            {
                runtime.Start("RemotingProcessBundle");

                Assert.AreEqual("RemotingProcessBundle", runtime.BundleContextes.Select(i => i.Bundle.Id).FirstOrDefault());

            }
        }

        [TestMethod]
        public void Test_Start_RemotingThreadBundle_With_Config()
        {
            using (var runtime = new BundleRuntime<IBundle>())
            {
                runtime.Start("RemotingThreadBundle");

                Assert.AreEqual("RemotingThreadBundle", runtime.BundleContextes.Select(i => i.Bundle.Id).FirstOrDefault());

            }
        }

        [TestMethod]
        public void Test_Run_The_Same_Bundle_Instances_In_Multi_Runtime_At_The_Same_Time()
        {
            var runtime1 = new BundleRuntime<IBundle>();
            var runtime2 = new BundleRuntime<IBundle>();

            try
            {
                runtime1.Start("Bundle");
                runtime2.Start("Bundle");


                runtime1.Start("RemotingThreadBundle");
                runtime2.Start("RemotingThreadBundle");


                runtime1.Start("RemotingProcessBundle");
                runtime2.Start("RemotingProcessBundle");
            }
            finally
            {
                runtime1.Dispose();
                runtime2.Dispose();
            }

        }


        #region Helpers
        [Export("Bundle", typeof(IBundle))]
        public class MyBundle : IBundle
        {
            public IBundleContext BundleContext
            {
                get;
                set;
            }

            public void Start()
            {
                //throw new NotImplementedException();
            }

            public void Stop()
            {
               // throw new NotImplementedException();
            }

         

            public string Id
            {
                get { return "Bundle"; }
            }

            public string Name
            {
                get { throw new NotImplementedException(); }
            }

            public string Description
            {
                get { throw new NotImplementedException(); }
            }

            public Version Version
            {
                get { throw new NotImplementedException(); }
            }


            public BundleState State
            {
                get { throw new NotImplementedException(); }
            }


            public string StartupPath
            {
                set { throw new NotImplementedException(); }
            }
        }

        [Export("RemotingThreadBundle", typeof(IBundle))]
        public class RemotingThreadBundle : IBundle
        {
            public IBundleContext BundleContext
            {
                get;
                set;
            }

            public void Start()
            {
                //throw new NotImplementedException();
            }

            public void Stop()
            {
               // throw new NotImplementedException();
            }

            public string Id
            {
                get { return "RemotingThreadBundle"; }
            }

            public string Name
            {
                get { throw new NotImplementedException(); }
            }

            public string Description
            {
                get { throw new NotImplementedException(); }
            }

            public Version Version
            {
                get { throw new NotImplementedException(); }
            }


            public BundleState State
            {
                get { throw new NotImplementedException(); }
            }


            public string StartupPath
            {
                set { throw new NotImplementedException(); }
            }
        }

        [Export("RemotingProcessBundle", typeof(IBundle))]
        public class RemotingProcessBundle : IBundle
        {
            public IBundleContext BundleContext
            {
                get;
                set;
            }

            public void Start()
            {
               // throw new NotImplementedException();
            }

            public void Stop()
            {
               // throw new NotImplementedException();
            }

            public string Id
            {
                get { return "RemotingProcessBundle"; }
            }

            public string Name
            {
                get { throw new NotImplementedException(); }
            }

            public string Description
            {
                get { throw new NotImplementedException(); }
            }

            public Version Version
            {
                get { throw new NotImplementedException(); }
            }


            public BundleState State
            {
                get { throw new NotImplementedException(); }
            }


            public string StartupPath
            {
                set { throw new NotImplementedException(); }
            }
        }
        #endregion
    }



}
